using System;
using System.Drawing;
using System.Windows.Forms;
using ClientTools.Library.DataTypes.Stats;
using ClientTools.Library.Interfaces;

namespace ClientTools.Client.Controls.Stats
{
    public partial class UcStats : BaseUserControl, IUserControl
    {

        #region Private Variables
        private IStats actStats;
        #endregion

        public UcStats()
        {
            InitializeComponent();
        }

        private void TsbGetStatsClick(object sender, EventArgs e)
        {
            GetStats();
        }

        private void CmdClearStatsByDateClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;

            StatsInfo si;
            try
            {
                si = actStats.DeleteStatsByDate(DateTimePicker1.Value);
            }
            catch (Exception ex)
            {
                ClearData();
                Enabled = false;
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            lblMessage1.Text = string.Format("Deleted {0} sessions with {1} entries...", si.Sessions, si.Entries);
            GetStats();

            Cursor = Cursors.Default;
        }

        private void ClearData()
        {
            Label1.Text = "-";
            lblMessage1.Text = "";
            pnlContent.Enabled = false;
        }

        private void GetStats()
        {
            Cursor = Cursors.WaitCursor;

            ClearData();
            StatsInfo si;
            try
            {
                si = actStats.GetStatsInfo();
            }
            catch (Exception ex)
            {
                ClearData();
                Enabled = false;
                Cursor = Cursors.Default;
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Label1.Text = string.Format("There are {0} sessions with {1} entries in the database...", si.Sessions, si.Entries);
            pnlContent.Enabled = true;

            Cursor = Cursors.Default;
        }

        #region I_UserControl Member

        public string Header
        {
            get { return "Statistics"; }
        }

        public void SetData(Library.Settings.Profile profile)
        {
            ActProfile = profile;
            ActProfile.InitProvider();
            ActProvider = ActProfile.ProviderObject;

            if (!ImplementsStats())
            {
                Enabled = false;
                return;
            }

            actStats = (IStats)ActProvider;
            ClearData();
            Enabled = true;
        }

        public Image ButtonImage
        {
            get { return tsbStats.Image; }
        }

        public int Sort
        {
            get { return 3; }
        }

        public bool ShowDelimiter
        {
            get { return false; }
        }

        public string ButtonText
        {
            get { return "Statistics"; }
        }

        #endregion
    }
}
